<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px" @submit.native.prevent>
      <el-form-item label="风格名称" prop="styleName">
        <el-input
          v-model="queryParams.styleName"
          placeholder="请输入风格名称"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型:" prop="type" label-width="110px">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="item in dict.type.sound_type"
            :key="parseInt(item.value)"
            :label="item.label"
            :value="parseInt(item.value)">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['human:speakerType:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="speakerTypeList">
      <el-table-column label="序号" type="index" fixed></el-table-column>
      <el-table-column label="风格名称" align="center" prop="styleName" />
      <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="描述" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['human:speakerType:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['human:speakerType:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改租户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="300px" @submit.native.prevent>
        <el-form-item label="风格名称:" prop="styleName" label-width="110px">
          <el-input v-model="form.styleName" placeholder="请输入风格名称"/>
        </el-form-item>
        <el-form-item label="类型:" prop="type" label-width="110px">
          <el-select v-model="form.type" placeholder="请选择类型" clearable>
            <el-option
              v-for="item in dict.type.sound_type"
              :key="parseInt(item.value)"
              :label="item.label"
              :value="parseInt(item.value)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述:" prop="remark" label-width="110px">
          <el-input type= "textarea" v-model="form.remark" :rows="4"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top: -50px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSpeakerTypeInfo, insertSpeakerType, deleteSpeakerType, updateSpeakerType, selectSpeakerType} from "@/api/platform/human/speakerType";

export default {
  name: "SpeakerType",
  dicts: ['sound_type'],
  data() {
    return {
      operationType: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      speakerTypeList: [],
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      detailOpen: false,
      // 是否显示添加修改弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        styleName: [
          { required: true, message: "风格名称不能为空", trigger: "blur" }
        ],
        type:[
          { required: true, message: "类别不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模板列表 */
    getList() {
      this.loading = true;
      listSpeakerTypeInfo(this.queryParams).then(response => {
        this.speakerTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发音人风格";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改发音人风格";
      selectSpeakerType(row.uid).then(response =>{
        this.form = response.data;
      })

    },
    typeFormat(row, index){
      if (row.type === 0) {
        return "发音人类型";
      } else if(row.type === 1){
        return "聊天背景音乐";
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uid == undefined) {
            let data = this.form;
            //提示
            const message = "<div>确定要新增配置信息？</div>"
            this.$modal.confirm(message).then(function() {
              return insertSpeakerType(data);
            }).then(() => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            })
          }else{
            let data = this.form;
            //提示
            const message = "<div>确定要修改配置信息？</div>"
            this.$modal.confirm(message).then(function() {
              return updateSpeakerType(data);
            }).then(() => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let data = {};
      data.uid = row.uid;
      data.type = row.type;
      //提示
      const message = "<div>确定要删除应用: <span>" + row.styleName + " ?</span></div>"+
        "<span style='margin-top: 20px'>应用ID:</span> &nbsp;&nbsp;&nbsp;" + row.uid
      this.$modal.confirm(message).then(function() {
        return deleteSpeakerType(data);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList()
      })
    },
  }
};
</script>
